package com.itwlj.controller.admin;

import com.itwlj.dto.GoodsDTO;
import com.itwlj.dto.GoodsPageQueryDTO;
import com.itwlj.result.PageResult;
import com.itwlj.result.Result;
import com.itwlj.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品管理
 */
@RestController
@RequestMapping("admin/goods")
@Api(tags = "商品相关接口")
@Slf4j
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 新增商品
     *
     * @param goodsDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增商品")
    public Result<String> insert(@RequestBody GoodsDTO goodsDTO) {
        log.info("新增商品：{}", goodsDTO);
        goodsService.insert(goodsDTO);
        return Result.success();
    }

    /**
     * 商品分页查询
     *
     * @param goodsPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("商品分页查询")
    public Result<PageResult> page(GoodsPageQueryDTO goodsPageQueryDTO) {
        log.info("商品分页查询：{}", goodsPageQueryDTO);
        PageResult pageResult = goodsService.pageQuery(goodsPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除商品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除商品")
    public Result<String> delete(@RequestParam List<Long> ids) {
        log.info("删除商品：{}", ids);
        goodsService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改商品
     *
     * @param goodsDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改商品")
    public Result<String> update(@RequestBody GoodsDTO goodsDTO) {
        log.info("修改商品：{}", goodsDTO);
        goodsService.update(goodsDTO);
        return Result.success();
    }

    /**
     * 商品起售停售
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("商品起售停售")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        log.info("商品起售停售：{}，状态：{}", id,status);
        goodsService.startOrStop(status, id);
        return Result.success();
    }
}
